<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="会员名称" prop="username">
      <el-input v-model="dataForm.username" placeholder="会员名称"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-radio v-model="dataForm.gender" label="1">男</el-radio>
      <el-radio v-model="dataForm.gender" label="2">女</el-radio>
      <el-radio v-model="dataForm.gender" label="0">未知</el-radio>
    </el-form-item>
    <el-form-item label="出生日期" prop="birthday">
      <el-date-picker
        v-model="dataForm.birthday"
        type="date"
        placeholder="选择日期"
        value-format="yyyy-MM-dd HH:mm:ss"
        :picker-options="pickerOptions1"
      >
      </el-date-picker>
    </el-form-item>
    <el-form-item label="会员等级" prop="userLevelId">
      <el-select v-model="dataForm.userLevelId" filterable placeholder="请选择">
        <el-option
          v-for="item in userLevels"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="手机号码" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号码"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        pickerOptions1: {
          disabledDate (time) {
            return time.getTime() > Date.now()
          }
        },
        dataForm: {
          id: 0,
          username: '',
          gender: '0',
          birthday: '',
          userLevelId: '',
          mobile: ''
        },
        userLevels: [],
        dataRule: {
          username: [
            { required: true, message: '会员名称不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '请选择性别', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '请选择出生日期', trigger: 'blur' }
          ],
          userLevelId: [
            { required: true, message: '请选择会员等级', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.getUserLevels()
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/platform/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.data.username
                this.dataForm.gender = data.data.gender + ''
                this.dataForm.birthday = data.data.birthday
                this.dataForm.userLevelId = data.data.userLevelId
                this.dataForm.mobile = data.data.mobile
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/platform/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'username': this.dataForm.username,
                'gender': this.dataForm.gender,
                'birthday': this.dataForm.birthday,
                'mobile': this.dataForm.mobile,
                'userLevelId': this.dataForm.userLevelId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      getUserLevels () {
        this.userLevels = []
        this.$http({
          url: this.$http.adornUrl('/platform/userlevel/listAll'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.userLevels = data.data
            console.log(this.userLevels)
          } else {
            this.userLevels = []
          }
        })
      }
    }
  }
</script>
